/**
 * 详情对话框
 */
var FunctionInfoDlg = {
    data: {
        id: "",
        name: ""
    }
};

layui.use(['layer', 'form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;

    var pid = system.getUrlParam("id");
    //改变当前pid的值，以及展示菜单展现方式
    pid && pid != 0 ? $("#pid").val(pid) : '';

    form.on('switch(url-switch)', function(data){
        if(data.elem.checked){
            $("#urlHome").attr("class" ,"layui-input-block");
            $("#urlHome").show();
        }else{
            $("#urlHome").hide();
            $("#url").val("#");
        }
        form.render();
    });

    // 点击url 选择功能
    $('#url').click(function () {
        var formName = encodeURIComponent("parent.FunctionInfoDlg.data.name");
        var formId = encodeURIComponent("parent.FunctionInfoDlg.data.id");
        var treeUrl = encodeURIComponent("/develop/function/search/tree");

        layer.open({
            type: 2,
            title: '功能菜单',
            area: ['70%', '90%'],
            content: system.contextPath + '/admin/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                if(FunctionInfoDlg.data.name.indexOf("/") == -1){
                    system.error("请选择具体的功能！");
                    return;
                }

                var url = FunctionInfoDlg.data.name;
                if(FunctionInfoDlg.data.name.indexOf(",") != 1){
                    var urls = FunctionInfoDlg.data.name.split(",");
                    for(var i = 0 ;i < urls.length ;++i){
                        if(urls[i] && urls[i].length > 1){
                            url = urls[i];
                            break;
                        }
                    }
                }
                $("#url").val(url);
            }
        });
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(system.contextPath + "/admin/menu/add", function (res) {
            if(res.status){
                system.success("添加成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            }else{
                system.error("添加失败！" + res.msg);
            }
        }, function (res) {
            system.error("添加失败！" + res.msg);
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
});